package com.mobile.challenge.ui.report.unit.manager.daily

import android.app.Application
import android.graphics.Color
import androidx.lifecycle.MutableLiveData
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mobile.challenge.api.ManagerOneDayService
import com.mobile.challenge.api.UserOneDayDataService
import com.mobile.challenge.exception.MobileException
import com.mobile.challenge.ui.report.utils.IntegerFormat
import com.mobile.challenge.user.ClockHelper
import com.mobile.challenge.user.UserHelper
import com.mobile.challenge.util.DateUtil
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList

class ManagerUnitDailyReportViewModel @Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {

    var pieData = MutableLiveData<PieData>()
    var totalCount = 0

    fun generateData(calendar: Calendar) {
        val subscribe = getRetrofitService(ManagerOneDayService::class.java)
                .getData(UserHelper.getInstance().getUserId(),
                        DateUtil.getFormatDateStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        // 数据
                        val myReportList = ArrayList<PieEntry>(0).apply {
                            if (data.nacount != 0) {
                                add(PieEntry(data.nacount.toFloat(), "NA"))
                            }

                            if (data.officeCount != 0) {
                                add(PieEntry(data.officeCount.toFloat(), "Office"))
                            }

                            if (data.leaveCount != 0) {
                                add(PieEntry(data.leaveCount.toFloat(), "Vacation"))
                            }

                            if (data.workHomeCount != 0) {
                                add(PieEntry(data.workHomeCount.toFloat(), "WFH"))
                            }
                        }
                        totalCount = 1
                        updateLiveData(myReportList)
                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        updateLiveData(ArrayList(0))
                        totalCount = 0
                    }
                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"
                    updateLiveData(ArrayList(0))
                    totalCount = 0
                })
    }

    private fun updateLiveData(myReportList: ArrayList<PieEntry>) {
        val dataSet = PieDataSet(myReportList, "My Report")
        // 颜色数据
        val dataColors = ArrayList<Int>(0).apply {
            add(Color.parseColor("#f38181"))
            add(Color.parseColor("#fce38a"))
            add(Color.parseColor("#eaffd0"))
            add(Color.parseColor("#95e1d3"))
        }
        dataSet.setDrawValues(true)
        dataSet.colors = dataColors
        dataSet.valueTextSize = 14F
        dataSet.valueFormatter = IntegerFormat()

        pieData.value = PieData(dataSet)
    }

}